django.jQuery(function() {
    django.jQuery("#id_type").change(function(event) {
        var type = django.jQuery(this).val();
        if (type) {
            get_subtypes(type);
        } else {
            var select = '<option value="">---------</option>';
            django.jQuery("#id_subtype").html(select);
        }
        });
        
    django.jQuery("#id_region").change(function(event) {
        var region = django.jQuery(this).val();
        if (region) {
            get_district(region);
        } else {
            var select = '<option value="">---------</option>';
            django.jQuery("#id_district").html(select);
        }
        django.jQuery("#id_district").trigger('change');
        });
        
    django.jQuery("#id_district").change(function(event) {
        var district = django.jQuery(this).val();
        var region = django.jQuery("#id_region").val();
        if (region && district) {
            get_suburb(region, district);
        } else {
            var select = '<option value="">---------</option>';
            django.jQuery("#id_suburb").html(select);
        }
        });
        
    django.jQuery("#id_type").trigger('change');
    django.jQuery("#id_region").trigger('change');
//    django.jQuery("#id_district").trigger('change');
});

function get_subtypes(type) {
    var options = {
        url: '/api/subtype/',
        data: {'type': type},
        success: function(data, textStatus, XMLHttpRequest) {
            var select = '<option value="">---------</option>';
            if (data.error) {
                alert(data.error);
                return
            };
            django.jQuery.each(data.response, function(index, opt) {
                select += '<option value="' + opt[0] + '">' + opt[1] + '</option>';
            });
            subtype_text = django.jQuery("#id_subtype option:selected").text();
            django.jQuery("#id_subtype").html(select);
            django.jQuery("#id_subtype option").filter(function() {
                return django.jQuery(this).text() == subtype_text;
            }).attr('selected', 'selected');
        },
        dataType: "json",
        type: "GET",
        timeout: 20000
     };
     django.jQuery.ajax(options);
}

function get_district(region) {
    var options = {
        url: '/api/district/',
        data: { 'region': region},
        success: function(data, textStatus, XMLHttpRequest) {
            var select = '<option value="">---------</option>';
            if (data.error) {
                alert(data.error);
                return
            };
            django.jQuery.each(data.response, function(index, opt) {
                select += '<option value="' + opt[0] + '">' + opt[1] + '</option>';
            });
            district_text = django.jQuery("#id_district option:selected").text();
            django.jQuery("#id_district").html(select);
            django.jQuery("#id_district option").filter(function() {
                return django.jQuery(this).text() == district_text;
            }).attr('selected', 'selected');
//            django.jQuery("#id_suburb").html('<option value="">---------</option>');
        },
        dataType: "json",
        type: "GET",
        timeout: 20000
     };
     django.jQuery.ajax(options);
}

function get_suburb(region, district) {
    var options = {
        url: '/api/suburb/',
        data: {'region': region, 'district': district},
        success: function(data, textStatus, XMLHttpRequest) {
            var select = '<option value="">---------</option>';
            if (data.error) {
                alert(data.error);
                return
            };
            django.jQuery.each(data.response, function(index, opt) {
                select += '<option value="' + opt[0] + '">' + opt[1] + '</option>';
            });
            suburb_text = django.jQuery("#id_suburb option:selected").text();
            django.jQuery("#id_suburb").html(select);
            django.jQuery("#id_suburb option").filter(function() {
                return django.jQuery(this).text() == suburb_text;
            }).attr('selected', 'selected');
        },
        dataType: "json",
        type: "GET",
        timeout: 20000
     };
     django.jQuery.ajax(options);
}
